var _ = require('lodash');
var path = require("path");
var dao = require(path.join(process.cwd(),"dao/DAO"));
var epidemicDao = require(path.join(process.cwd(),"dao/epidemicDao"));
// const ExcelJS = require('exceljs');

/**
 * 添加返万人员
 *
 * @param  {[type]}   params [description]
 * @param  {Function} cb     [description]
 */
module.exports.getReturnLists = function(conditions,cb) {
    if(!conditions.pagenum) return cb("pagenum 参数不合法");
    if(!conditions.pagesize) return cb("pagesize 参数不合法");
    // 通过关键词获取管理员数量

    epidemicDao.countByKey(conditions["type"],conditions["query"],function(err,count) {
        console.log(conditions["query"]);

        key = conditions["query"];
        pagenum = parseInt(conditions["pagenum"]);
        pagesize = parseInt(conditions["pagesize"]);
        type=conditions["type"]

        pageCount = Math.ceil(count / pagesize);
        offset = (pagenum - 1) * pagesize;
        if(offset >= count) {
            offset = count;
        }
        limit = pagesize;

        epidemicDao.findByKey(key,offset,limit,type,function(err,members){
            var retMembers = [];
            for(idx in members) {
                var member = members[idx];

                retMembers.push({
                    "i":member.id,
                    "id_card": member.id_card,
                    "user_name":member.user_name,
                    "sex":member.sex,
                    "tel":member.tel,
                    "come":member.come,
                    "to":member.to,
                    "arrive_time":member.arrive_time,
                    "addr":member.addr,
                    "measure":member.measure,
                    "fever":member.fever,
                    "rehosipital":member.rehosipital,
                    "nucleic":member.nucleic,
                    "create_person":member.create_person,
                    "create_time":member.create_time,
                    "remark":member.remark,


                });
            }
            var resultDta = {};
            resultDta["total"] = count;
            resultDta["pagenum"] = pagenum;
            resultDta["persons"] = retMembers;
            cb(err,resultDta);
        });
    });






}

/**
 * 通过创建人返回列表
 *
 * @param  {[type]}   params [description]
 * @param  {Function} cb     [description]
 */
module.exports.getReturnLists1 = function(conditions,cb) {
    if(!conditions.pagenum) return cb("pagenum 参数不合法");
    if(!conditions.pagesize) return cb("pagesize 参数不合法");
    // 通过关键词获取管理员数量

    epidemicDao.countByKey(conditions["type"],conditions["query"],function(err,count) {
        console.log(conditions["query"]);

        key = conditions["query"];
        pagenum = parseInt(conditions["pagenum"]);
        pagesize = parseInt(conditions["pagesize"]);
        type=conditions["type"]

        pageCount = Math.ceil(count / pagesize);
        offset = (pagenum - 1) * pagesize;
        if(offset >= count) {
            offset = count;
        }
        limit = pagesize;

        epidemicDao.findByUserNameKey(key,offset,limit,type,function(err,members){
            var retMembers = [];
            for(idx in members) {
                var member = members[idx];

                retMembers.push({
                    "i":member.id,
                    "id_card": member.id_card,
                    "user_name":member.user_name,
                    "sex":member.sex,
                    "tel":member.tel,
                    "come":member.come,
                    "to":member.to,
                    "arrive_time":member.arrive_time,
                    "addr":member.addr,
                    "measure":member.measure,
                    "fever":member.fever,
                    "rehosipital":member.rehosipital,
                    "nucleic":member.nucleic,
                    "create_person":member.create_person,
                    "create_time":member.create_time,
                    "remark":member.remark,


                });
            }
            var resultDta = {};
            resultDta["total"] = count;
            resultDta["pagenum"] = pagenum;
            resultDta["persons"] = retMembers;
            cb(err,resultDta);
        });
    });






}


/**
 * 创建成员
 *
 * @param  {[type]}   obj 管理员信息
 * @param  {Function} cb  回调函数
 */
module.exports.addEpidemic = function (obj, cb) {
    let arrive_time =new Date(obj.arrive_time);
    let y = arrive_time.getFullYear()
    let m = arrive_time.getMonth()+1
    let d = arrive_time.getDate()
    obj.arrive_time= `${y}-${m}-${d}`
  let   ctime = new Date()
    let cy = ctime.getFullYear()
    let cm = ctime.getMonth()+1
    let cd = ctime.getDate()
    let chh = ctime.getHours()
    let cmm = ctime.getMinutes()
    let css = ctime.getSeconds()
    obj.create_time= `${cy}-${cm}-${cd} ${chh}:${cmm}:${css}`
    console.log(obj);
    console.log('***************');

    epidemicDao.addMember(obj, cb);
}

module.exports.deleteByID = function(user_id,cb){
    epidemicDao.deleteByUserID(user_id,cb)
}


module.exports.updateMember = function(id,params,cb) {

    let arrive_time =new Date(params.arrive_time);
    let y = arrive_time.getFullYear()
    let m = arrive_time.getMonth()+1
    let d = arrive_time.getDate()
    params.arrive_time= `${y}-${m}-${d}`
    let   ctime = new Date()
    let cy = ctime.getFullYear()
    let cm = ctime.getMonth()+1
    let cd = ctime.getDate()
    let chh = ctime.getHours()
    let cmm = ctime.getMinutes()
    let css = ctime.getSeconds()
    params.create_time= `${cy}-${cm}-${cd} ${chh}:${cmm}:${css}`
    console.log(params);

    epidemicDao.update(id,params,function(err,member){
        if(err) {
            console.log(err);
            return cb("更新失败");}
        result = "更新失败";
        cb(null,result);
    });
}


module.exports.getMemberById = function(id,cb) {
    epidemicDao.findByID(id, cb)
}
module.exports.listexport = async function (id, cb) {



//
//     const workbook = new ExcelJS.Workbook();
//     workbook.creator = 'Me';
//     workbook.lastModifiedBy = 'Her';
//     workbook.created = new Date(1985, 8, 30);
//     workbook.modified = new Date();
//     workbook.lastPrinted = new Date();
//     workbook.views = [
//         {
//             x: 0, y: 0, width: 10000, height: 20000,
//             firstSheet: 0, activeTab: 1, visibility: 'visible'
//         }
//     ]
//
//     const sheet = workbook.addWorksheet('My Sheet');
//
//     const worksheet = workbook.addWorksheet('My Sheet1', {
//         pageSetup: {paperSize: 9, orientation: 'landscape'}
//     });
//
//     workbook.eachSheet(function (worksheet, sheetId) {
//         // ...
//     });
//
// // 写入文件
//     let filename ="F:/vue_shop/server/services/wcs.xlsx"
//     await workbook.xlsx.writeFile(filename);
    cb(null,'成功')
}
